<template>
  <!-- 搜索历史 -->
  <div>
    <van-cell title="搜索历史">
      <van-icon v-if="!isDelete" name="delete-o" @click="isDelete = true"></van-icon>
      <template v-else>
        <span @click="deleteAll">全部删除</span>
        <span style="margin-left:10px" @click="isDelete = false">完成</span>
      </template>
    </van-cell>
    <!-- 记录 -->
    <van-cell v-for="(item,i) in historyList" :key="item" :title="item" @click="historyDelete(item,i)">
      <van-icon v-if="isDelete" name="close"></van-icon>
    </van-cell>

    <!-- 暂无历史 -->
    <van-empty v-if="!historyList.length" image="error" description="暂无搜索历史" />
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 删除状态
      isDelete: false
    }
  },
  props: {
    historyList: {
      type: Array,
      require: true
    }
  },
  methods: {
    // 全部删除
    deleteAll () {
      this.historyList.splice(0)
    },
    // 点击搜索历史
    historyDelete (item, index) {
      // 删除状态下删除历史
      if (this.isDelete) {
        this.historyList.splice(index, 1)
      } else {
        // 非删除状态下搜索
        this.$emit('search', item)
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
